package myta.bean;

import java.util.Map;

import javax.faces.component.html.HtmlInputText;

import myta.base.FormDataBase;
import myta.model.DataRow;
import myta.model.DataTable;
import myta.service.Command;
import myta.service.SQLexec;

public class JurusanData extends FormDataBase{
	private HtmlInputText txtid;
	private HtmlInputText txtnama;
	
	@Override
	public void init() throws Exception {
		formName = "jurusan";
		
		txtid = new HtmlInputText();
		txtnama = new HtmlInputText();
	}

	@Override
	public void generateID() throws Exception {
		String sql = "call idjurusan()";
		Command command = SQLexec.makeCommand(sql);
		DataTable datatable = command.executeQuery();
		DataRow datarow = datatable.getRow(0);
		txtid.setValue(datarow.getString("id"));
	}

	@Override
	public void getRequestData(Map<String, String> map) throws Exception {
		if(map.containsKey("idjurusan")){
			String idjurusan = map.get("idjurusan");
			
			String sql = "select * from jurusan where idjurusan = ? order by idjurusan desc";
			Command command = SQLexec.makeCommand(sql);
			command.setObject(1, idjurusan);
			DataTable datatable = command.executeQuery();
			DataRow datarow = datatable.getRow(0);
			
			txtid.setValue(datarow.getString("idjurusan"));
			txtnama.setValue(datarow.getString("namajurusan"));
		}
	}

	@Override
	public boolean simpan() throws Exception {
		boolean boo = false;
		
		String sql = "INSERT INTO jurusan(idjurusan,namajurusan) VALUES(?,?);";
		Command command = SQLexec.makeCommand(sql);
		command.setObject(1, txtid.getValue().toString());
		command.setObject(2, txtnama.getValue().toString());
		boo = command.executeUpdate();
		
		if(boo){
			return true;
		}else{
			return false;
		}		
	}

	@Override
	public boolean ubah() throws Exception {
		boolean boo = false;
		
		String sql = "UPDATE jurusan SET namajurusan=? where idjurusan = ?;";
		Command command = SQLexec.makeCommand(sql);
		command.setObject(1, txtnama.getValue().toString());
		command.setObject(2, txtid.getValue().toString());
		boo = command.executeUpdate();
		
		if(boo){
			return true;
		}else{
			return false;
		}
	}

	public HtmlInputText getTxtid() {
		return txtid;
	}

	public void setTxtid(HtmlInputText txtid) {
		this.txtid = txtid;
	}

	public HtmlInputText getTxtnama() {
		return txtnama;
	}

	public void setTxtnama(HtmlInputText txtnama) {
		this.txtnama = txtnama;
	}
}
